package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.ListNode;
import org.springframework.util.Assert;

/**
 * @ClassName ReverseList
 * @Description ReverseList
 * @Author wanghaiwei
 * @Date 2023/1/12 10:11
 */
public class ReverseList {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode tmp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = tmp;
        }
        return prev;
    }

    public static void main(String[] args) {
        ReverseList solution = new ReverseList();
        String result = solution.reverseList(ListNode.convertArrayToListNode(new int[]{1,2,3,4,5})).toString();
        Assert.isTrue(result.equals("5,4,3,2,1"), "[1,2,3,4,5]计算错误,实际结果:"+result);
        result = solution.reverseList(ListNode.convertArrayToListNode(new int[]{1})).toString();
        Assert.isTrue(result.equals("1"), "[1]计算错误,实际结果:"+result);
    }
}
